package com.wb.mas.app;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;

import com.appsflyer.AppsFlyerConversionListener;
import com.appsflyer.AppsFlyerLib;
import com.blankj.utilcode.util.SPUtils;
import com.facebook.stetho.Stetho;
import com.wb.mas.BuildConfig;
import com.wb.mas.R;

import java.util.Map;

import me.goldze.mvvmhabit.base.AppManager;
import me.goldze.mvvmhabit.base.BaseApplication;
import me.goldze.mvvmhabit.crash.CaocConfig;
import me.goldze.mvvmhabit.utils.KLog;

/**
 * Created by goldze on 2017/7/16.
 */

public class AppApplication extends BaseApplication {


    @Override
    public void onCreate() {
        super.onCreate();
        //是否开启打印日志
        KLog.init(BuildConfig.DEBUG);
        //初始化全局异常崩溃
        initCrash();
        //内存泄漏检测
//        if (!LeakCanary.isInAnalyzerProcess(this)) {
//            LeakCanary.install(this);
//        }
        if (BuildConfig.isDebug) {
            Stetho.initializeWithDefaults(this);
        }
        initAppFlyer();
    }

    private void initCrash() {
        CaocConfig.Builder.create()
                .backgroundMode(CaocConfig.BACKGROUND_MODE_SILENT) //背景模式,开启沉浸式
                .enabled(true) //是否启动全局异常捕获
                .showErrorDetails(true) //是否显示错误详细信息
                .showRestartButton(true) //是否显示重启按钮
                .trackActivities(true) //是否跟踪Activity
                .minTimeBetweenCrashesMs(2000) //崩溃的间隔时间(毫秒)
                .errorDrawable(R.mipmap.ic_launcher) //错误图标
//                .restartActivity(LoginActivity.class) //重新启动后的activity
//                .errorActivity(YourCustomErrorActivity.class) //崩溃后的错误activity
//                .eventListener(new YourCustomEventListener()) //崩溃后的错误监听
                .apply();
    }



    /**
     * 跳转
     *
     * @param intent
     */
    public static void goActivity(Intent intent) {
        try {
            Activity activity = AppManager.getAppManager().getActivityStack().peek();
            if (activity != null) {
                activity.startActivity(intent);
            } else {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getInstance().startActivity(intent);
            }
        } catch (Exception e) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getInstance().startActivity(intent);
        }

    }


    public static Context getActivityContext(){
        try {
            Activity activity = AppManager.getAppManager().getActivityStack().peek();
            if (activity != null) {
                return activity;
            } else {
                return getInstance();
            }
        } catch (Exception e) {
            return getInstance();
        }
    }


    public void initAppFlyer() {
        AppsFlyerConversionListener conversionDataListener =
                new AppsFlyerConversionListener() {
                    @Override
                    public void onInstallConversionDataLoaded(Map<String, String> map) {

                        KLog.i("onInstallConversionDataLoaded");
                    }

                    @Override
                    public void onInstallConversionFailure(String s) {
                        KLog.i("onInstallConversionFailure:" + s);

                    }

                    @Override
                    public void onAppOpenAttribution(Map<String, String> map) {
                        KLog.i("onAppOpenAttribution");

                    }

                    @Override
                    public void onAttributionFailure(String s) {
                        KLog.i("onAttributionFailure");

                    }
                };
        AppsFlyerLib.getInstance().init(getString(R.string.AF_DEV_KEY), conversionDataListener, getApplicationContext());
        AppsFlyerLib.getInstance().startTracking(this);
        String appsFlyerUID = AppsFlyerLib.getInstance().getAppsFlyerUID(getApplicationContext());
        SPUtils.getInstance().put(SPKeys.APPS_FLYER_UID, appsFlyerUID);
        KLog.i("appsFlyerUID:" + appsFlyerUID);
    }


    public static int getStatusBarHeight() {
        Resources resources = getInstance().getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }
}

